﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace BackupRestoreSoftwares.FormChild
{
    public partial class m_Tools : UserControl
    {
        private readonly string App_path = Application.StartupPath;
        private readonly bool Is64 = Environment.Is64BitOperatingSystem;
        private readonly string Program_32 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
        private readonly string Program_64 = Path.GetPathRoot(Environment.SystemDirectory) + "Program Files";
        private readonly string Roaming_path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        private string Common_path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
        private string Local_path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

        public m_Tools()
        {
            InitializeComponent();
        }

        private void Export(string exportPath, string registryPath)
        {
            string path = "\"" + exportPath + "\"";
            string key = "\"" + registryPath + "\"";
            var proc = new Process();

            try
            {
                proc.StartInfo.FileName = "regedit.exe";
                proc.StartInfo.UseShellExecute = false;

                proc = Process.Start("regedit.exe", "/e " + path + " " + key + "");
                proc.WaitForExit();
            }
            catch (Exception)
            {
                proc.Dispose();
            }
        }

        private void Import(string registryPath)
        {
            string key = "\"" + registryPath + "\"";
            var proc = new Process();
            try
            {
                proc.StartInfo.FileName = "regedit.exe";
                proc.StartInfo.UseShellExecute = false;

                proc = Process.Start("regedit.exe", "/s " + key + "");
                proc.WaitForExit();
            }
            catch (Exception)
            {
                proc.Dispose();
            }
        }

        private void btn_bk_idm_Click(object sender, EventArgs e)
        {
            DialogResult result = EasyDialog.ShowYesNoBackup("Internet Download Manager");
            if (result == DialogResult.Yes)
            {
                try
                {
                    var t = new Thread(frmMain.waitscreen);
                    t.Start();

                    string chkfile = Program_32 + @"\Internet Download Manager\IDMan.exe";
                    if (File.Exists(chkfile))
                    {
                        foreach (Process app in Process.GetProcesses())
                            if (app.ProcessName == "IDMan")
                                app.Kill();
                        Export(App_path + @"\DBBACKUP\IDMSetting.reg", @"HKEY_CURRENT_USER\Software\DownloadManager");

                        t.Abort();
                        MessageBox.Show("Backup completed!", "Finish");
                    }
                    else
                    {
                        t.Abort();
                        MessageBox.Show("IDM don't install!", "Error");
                    }
                }
                catch
                {
                    MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
                }
            }
        }

        private void btn_res_idm_Click(object sender, EventArgs e)
        {
            DialogResult result = EasyDialog.ShowYesNoRestore("Internet Download Manager");
            if (result == DialogResult.Yes)
            {
                try
                {
                    var t = new Thread(frmMain.waitscreen);
                    t.Start();

                    string chkfile = Program_32 + @"\Internet Download Manager\IDMan.exe";
                    if (File.Exists(chkfile))
                    {
                        foreach (Process app in Process.GetProcesses())
                            if (app.ProcessName == "IDMan")
                                app.Kill();

                        Import(App_path + @"\DBBACKUP\IDMSetting.reg");
                        t.Abort();
                        MessageBox.Show("Restore completed!", "Finish");
                    }
                    else
                    {
                        t.Abort();
                        MessageBox.Show("IDM don't install!", "Error");
                    }
                }
                catch
                {
                    MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
                }
            }
        }

        private void btn_bk_winrar_Click(object sender, EventArgs e)
        {
            DialogResult result = EasyDialog.ShowYesNoBackup("Winrar");
            if (result == DialogResult.Yes)
            {
                try
                {
                    var t = new Thread(frmMain.waitscreen);
                    t.Start();

                    if (Is64)
                    {
                        string chkfile_64 = Program_64 + @"\WinRAR\WinRAR.exe";
                        string chkfile_32 = Program_32 + @"\WinRAR\WinRAR.exe";
                        if (File.Exists(chkfile_64) || File.Exists(chkfile_32))
                        {
                            foreach (Process app in Process.GetProcesses())
                                if (app.ProcessName == "WinRAR")
                                    app.Kill();
                            Export(App_path + @"\WinRARSetting.reg", @"HKEY_CURRENT_USER\Software\WinRAR");

                            t.Abort();
                            MessageBox.Show("Backup completed!", "Finish");
                        }
                        else
                        {
                            t.Abort();
                            MessageBox.Show("WinRAR don't install!", "Error");
                        }
                    }
                    else
                    {
                        string chkfile_32 = Program_32 + @"\WinRAR\WinRAR.exe";
                        if (File.Exists(chkfile_32))
                        {
                            foreach (Process app in Process.GetProcesses())
                                if (app.ProcessName == "WinRAR")
                                    app.Kill();
                            Export(App_path + @"\WinRARSetting.reg", @"HKEY_CURRENT_USER\Software\WinRAR");

                            t.Abort();
                            MessageBox.Show("Backup completed!", "Finish");
                        }
                        else
                        {
                            t.Abort();
                            MessageBox.Show("WinRAR don't install!", "Error");
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
                }
            }
        }

        private void btn_res_winrar_Click(object sender, EventArgs e)
        {
            DialogResult result = EasyDialog.ShowYesNoRestore("Winrar");
            if (result == DialogResult.Yes)
            {
                try
                {
                    var t = new Thread(frmMain.waitscreen);
                    t.Start();

                    if (Is64)
                    {
                        string chkfile_64 = Program_64 + @"\WinRAR\WinRAR.exe";
                        string chkfile_32 = Program_32 + @"\WinRAR\WinRAR.exe";
                        if (File.Exists(chkfile_64) || File.Exists(chkfile_32))
                        {
                            foreach (Process app in Process.GetProcesses())
                                if (app.ProcessName == "WinRAR")
                                    app.Kill();
                            Import(App_path + @"\WinRARSetting.reg");

                            t.Abort();
                            MessageBox.Show("Restore completed!", "Finish");
                        }
                        else
                        {
                            t.Abort();
                            MessageBox.Show("WinRAR don't install!", "Error");
                        }
                    }
                    else
                    {
                        string chkfile_32 = Program_32 + @"\WinRAR\WinRAR.exe";
                        if (File.Exists(chkfile_32))
                        {
                            foreach (Process app in Process.GetProcesses())
                                if (app.ProcessName == "WinRAR")
                                    app.Kill();
                            Import(App_path + @"\WinRARSetting.reg");

                            t.Abort();
                            MessageBox.Show("Restore completed!", "Finish");
                        }
                        else
                        {
                            t.Abort();
                            MessageBox.Show("WinRAR don't install!", "Error");
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
                }
            }
        }

        private void btn_bk_svn_Click(object sender, EventArgs e)
        {
            DialogResult result = EasyDialog.ShowYesNoBackup("TortoiseSVN");
            if (result == DialogResult.Yes)
            {
                if (File.Exists(App_path + @"\DBBACKUP\TortoiseSVN") != true)
                {
                    Directory.CreateDirectory(App_path + @"\DBBACKUP\TortoiseSVN");
                }
                try
                {
                    var t = new Thread(frmMain.waitscreen);
                    t.Start();

                    string chkfile = Program_64 + @"\TortoiseSVN\bin\TortoiseProc.exe";
                    if (File.Exists(chkfile))
                    {
                        string rar_file = App_path + @"\DBBACKUP\TortoiseSVN\Subversion.rar";
                        string path_file = Roaming_path + @"\Subversion\";
                        EasyRAR.Compress(rar_file, path_file);

                        t.Abort();
                        EasyDialog.ShowSuccessBackup("TortoiseSVN");
                    }
                    else
                    {
                        t.Abort();
                        EasyDialog.ShowNotinstall("TortoiseSVN");
                    }
                }
                catch
                {
                    EasyDialog.ShowContact();
                }
            }
        }

        private void btn_res_svn_Click(object sender, EventArgs e)
        {
            DialogResult result = EasyDialog.ShowYesNoRestore("TortoiseSVN");
            if (result == DialogResult.Yes)
            {
                try
                {
                    var t = new Thread(frmMain.waitscreen);
                    t.Start();

                    string chkfile = Program_64 + @"\TortoiseSVN\bin\TortoiseProc.exe";
                    if (File.Exists(chkfile))
                    {
                        string rar_file = App_path + @"\DBBACKUP\TortoiseSVN\Subversion.rar";
                        string path_file = Roaming_path + @"\Subversion\";
                        EasyRAR.Extract(rar_file, path_file);

                        t.Abort();
                        EasyDialog.ShowSuccessRestore("TortoiseSVN");
                    }
                    else
                    {
                        t.Abort();
                        EasyDialog.ShowNotinstall("TortoiseSVN");
                    }
                }
                catch
                {
                    EasyDialog.ShowContact();
                }
            }
        }
    }
}